package pe.gob.mpfn.model;

import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Entity
@Table(name = "expediente")
@Configurable
@RooJavaBean
@RooToString
@RooJpaActiveRecord(versionField = "", table = "expediente")
@RooDbManaged(automaticallyDelete = true)
public class Expediente {

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	@PersistenceContext
    transient EntityManager entityManager;

	public static final EntityManager entityManager() {
        EntityManager em = new Expediente().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countExpedientes() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Expediente o", Long.class).getSingleResult();
    }

	public static List<Expediente> findAllExpedientes() {
        return entityManager().createQuery("SELECT o FROM Expediente o", Expediente.class).getResultList();
    }

	public static Expediente findExpediente(String strNumeroExpedienteId) {
        if (strNumeroExpedienteId == null || strNumeroExpedienteId.length() == 0) return null;
        return entityManager().find(Expediente.class, strNumeroExpedienteId);
    }

	public static List<Expediente> findExpedienteEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Expediente o", Expediente.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Expediente attached = Expediente.findExpediente(this.strNumeroExpedienteId);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public Expediente merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Expediente merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "str_numero_expediente_id", columnDefinition = "VARCHAR", length = 12)
    private String strNumeroExpedienteId;

	public String getStrNumeroExpedienteId() {
        return this.strNumeroExpedienteId;
    }

	public void setStrNumeroExpedienteId(String id) {
        this.strNumeroExpedienteId = id;
    }

	@OneToMany(mappedBy = "strNumeroExpedienteId")
    private Set<Diligencia> diligencias;

	@OneToMany(mappedBy = "strNumeroExpedienteId")
    private Set<ExpedienteParte> expedientePartes;

	@OneToMany(mappedBy = "strNumeroExpedienteId")
    private Set<Informe> informes;

	@ManyToOne
    @JoinColumn(name = "int_ubigeo_suceso_id", referencedColumnName = "int_tabla_id", nullable = false)
    private TablaMaestra intUbigeoSucesoId;

	@ManyToOne
    @JoinColumn(name = "int_dependencia_id", referencedColumnName = "int_tabla_id", nullable = false)
    private TablaMaestra intDependenciaId;

	@ManyToOne
    @JoinColumn(name = "int_tipo_expediente_id", referencedColumnName = "int_tabla_id", nullable = false)
    private TablaMaestra intTipoExpedienteId;

	@Column(name = "str_lugar_suceso", columnDefinition = "VARCHAR", length = 100)
    private String strLugarSuceso;

	@Column(name = "dat_fecha_registro", columnDefinition = "DATETIME")
    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date datFechaRegistro;

	@Column(name = "dat_fecha_suceso", columnDefinition = "DATETIME")
    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date datFechaSuceso;

	public Set<Diligencia> getDiligencias() {
        return diligencias;
    }

	public void setDiligencias(Set<Diligencia> diligencias) {
        this.diligencias = diligencias;
    }

	public Set<ExpedienteParte> getExpedientePartes() {
        return expedientePartes;
    }

	public void setExpedientePartes(Set<ExpedienteParte> expedientePartes) {
        this.expedientePartes = expedientePartes;
    }

	public Set<Informe> getInformes() {
        return informes;
    }

	public void setInformes(Set<Informe> informes) {
        this.informes = informes;
    }

	public TablaMaestra getIntUbigeoSucesoId() {
        return intUbigeoSucesoId;
    }

	public void setIntUbigeoSucesoId(TablaMaestra intUbigeoSucesoId) {
        this.intUbigeoSucesoId = intUbigeoSucesoId;
    }

	public TablaMaestra getIntDependenciaId() {
        return intDependenciaId;
    }

	public void setIntDependenciaId(TablaMaestra intDependenciaId) {
        this.intDependenciaId = intDependenciaId;
    }

	public TablaMaestra getIntTipoExpedienteId() {
        return intTipoExpedienteId;
    }

	public void setIntTipoExpedienteId(TablaMaestra intTipoExpedienteId) {
        this.intTipoExpedienteId = intTipoExpedienteId;
    }

	public String getStrLugarSuceso() {
        return strLugarSuceso;
    }

	public void setStrLugarSuceso(String strLugarSuceso) {
        this.strLugarSuceso = strLugarSuceso;
    }

	public Date getDatFechaRegistro() {
        return datFechaRegistro;
    }

	public void setDatFechaRegistro(Date datFechaRegistro) {
        this.datFechaRegistro = datFechaRegistro;
    }

	public Date getDatFechaSuceso() {
        return datFechaSuceso;
    }

	public void setDatFechaSuceso(Date datFechaSuceso) {
        this.datFechaSuceso = datFechaSuceso;
    }
}
